﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace iMusik.Mp3ZingOnline
{
    public class ZingSearch
    {
        private string _Key;
        private List<Mp3Song> _ResultListSong;
        private List<Mp3Video> _ResultListVideo;

        public List<Mp3Song> ResultListSong
        {
            get { return _ResultListSong; }
            private set { _ResultListSong = value; }
        }
        public List<Mp3Video> ResultListVideo
        {
            get { return _ResultListVideo; }
            private set { _ResultListVideo = value; }
        }

        public ZingSearch(string key)
        {
            _Key = key;
            _ResultListSong = new List<Mp3Song>();
            _ResultListVideo = new List<Mp3Video>();
            
        }

        public async Task SetAllInformation()
        {
            try
            {
                _Key = _Key.Replace(' ', '+');
                if (_ResultListSong.Count == 0)
                    await SetListSong();
                if (_ResultListVideo.Count == 0)
                    await SetListVideo();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        private async Task SetListSong()
        {
            string source = await Mp3Zing.DownloadString(new Uri("http://m.mp3.zing.vn/tim-kiem/bai-hat.html?q="+_Key));
            int i = 0;
            while (i < source.Length)
            {
                if (source[i] == 34)
                {
                    source = source.Remove(i, 1);
                    i--;
                }
                i++;
            }

            List<string> listSongName = GetListName(source);
            List<string> listSingerName = GetListSingerName(source);
            List<string> listLocalLink = GetListLocaLinkSong(source);

            int j = 0;
            foreach(string link in listLocalLink)
            {
                _ResultListSong.Add(new Mp3Song(listSongName[j], listSingerName[j], link, true));
                j++;
            }

        }

        private async Task SetListVideo()
        {
            string source = await Mp3Zing.DownloadString(new Uri("http://m.mp3.zing.vn/tim-kiem/video.html?q=" + _Key));
            int i = 0;
            while (i < source.Length)
            {
                if (source[i] == 34)
                {
                    source = source.Remove(i, 1);
                    i--;
                }
                i++;
            }

            List<string> listVideoName = GetListName(source);
            List<string> listSingerName = GetListSingerName(source);
            List<string> listLocalLink = GetListLocaLinkVideo(source);
            List<string> listImgLink = GetListImgLink(source);
            int j = 0;
            foreach (string link in listLocalLink)
            {
                _ResultListVideo.Add(new Mp3Video(listVideoName[j], listSingerName[j], listImgLink[j], link));
                j++;
            }

        }

        private List<string> GetListSingerName(string source)
        {
            List<string> listSingerName = new List<string>();
            string pattern = @"(?<=<h4>).*?(?=<)";
            Regex regex3 = new Regex(pattern);
            MatchCollection mc3 = regex3.Matches(source);
            foreach (Match m1 in mc3)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSingerName.Add(temp2);

            }
            return listSingerName;
        }

        private List<string> GetListName(string source)
        {
            List<string> listSongAlbumName = new List<string>();
            string pattern = @"(?<=<h3>).*?(?=<)";
            Regex regex2 = new Regex(pattern);
            MatchCollection mc2 = regex2.Matches(source);
            foreach (Match m1 in mc2)
            {
                string temp2 = m1.Value;
                temp2 = Mp3Zing.ConvertStringAsciiToUtf8(temp2);
                listSongAlbumName.Add(temp2);

            }
            return listSongAlbumName;
        }

        private List<string> GetListLocaLinkSong(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/bai-hat).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/bai-hat";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }

        private List<string> GetListLocaLinkVideo(string source)
        {
            List<string> listSongLocalLink = new List<string>();
            string pattern = @"(?<=href=/video-clip).*?(html)";
            Regex regex1 = new Regex(pattern);
            MatchCollection mc1 = regex1.Matches(source);
            int j = 0;
            foreach (Match m1 in mc1)
            {
                string temp = "http://m.mp3.zing.vn/video-clip";
                temp += m1.Value;
                listSongLocalLink.Add(temp);
            }
            return listSongLocalLink;
        }

        private List<string> GetListImgLink(string source)
        {
            List<string> listLink = new List<string>();
            string pattern = @"(?=http://image).*?(jpg)";
            Regex regex = new Regex(pattern);
            MatchCollection mc = regex.Matches(source);
            int j;
            foreach (Match m1 in mc)
            {
                listLink.Add(m1.Value);
            }
            return listLink;
        }
    }
}
